﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Linq;

public partial class Publish_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + @"App_Data\latestnews.xml");
        DataTable dt = ds.Tables[0];
        rptLatestNews.DataSource = dt;
        rptLatestNews.DataBind();
        dt.Dispose();
        ds.Dispose();

        ds = new DataSet();
        ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + @"App_Data\latesthotnews.xml");
        dt = ds.Tables[0];
        rptHotNews.DataSource = dt;
        rptHotNews.DataBind();
        dt.Dispose();
        ds.Dispose();

        dt = GenerateRandom(4);
        rptProductRandom.DataSource = dt;
        rptProductRandom.DataBind();
        dt.Dispose();

        dt = GetListOfSupports();
        rptSupport.DataSource = dt;
        rptSupport.DataBind();
        dt.Dispose();

        dt = new DataTable("ProductType");
        DataColumn dc1 = new DataColumn("ProductType");
        DataColumn dc2 = new DataColumn("Title");
        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);
        DataRow dr = dt.NewRow();
        dr["ProductType"] = 1;
        dr["Title"] = "Sales Off";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["ProductType"] = 2;
        dr["Title"] = "New Arrival";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["ProductType"] = 3;
        dr["Title"] = "Hot Product";
        dt.Rows.Add(dr);
        rptProduct.DataSource = dt;
        rptProduct.DataBind();
    }

    protected string BindNews(string id, string title, string shortText, string image)
    {
        string rs = "";
        rs += "<table width='100%'><tr><td>";
        if (image != "")
        {
            rs += "<table align='left'><tr><td style='padding: 4px 10px 4px 4px;'><a href='NewsDetail.aspx?id=" + id + "'>";
            rs += "<img alt='' src='NewsThumbnail.aspx?id=" + id + "&width=90' border='0' align='left' onerror=\"this.src='../images/noimage90.gif'\" />";
            rs += "</a></td></tr></table>";
        }
        rs += "<p class='title'><a href='NewsDetail.aspx?id=" + id + "'>" + title + "</a></p>";
        rs += "<p>" + shortText + "</p>";
        rs += "<p class='detail'><a href='NewsDetail.aspx?id=" + id + "'>Chi ti&#7871;t</a></p>";
        rs += "</td></tr></table>";
        return rs;
    }

    protected string BindNews(string id, string title, string image)
    {
        string rs = "";
        rs += "<table width='100%'><tr><td>";
        if (image != "")
        {
            rs += "<table align='left'><tr><td style='padding: 3px 6px 3px 3px;'><a href='NewsDetail.aspx?id=" + id + "'>";
            rs += "<img alt='' src='NewsThumbnail.aspx?id=" + id + "&width=50' border='0' align='left' onerror=\"this.src='../images/noimage50.gif'\" />";
            rs += "</a></td></tr></table>";
        }
        rs += "<p class='title'><a href='NewsDetail.aspx?id=" + id + "'>" + title + "</a></p>";
        rs += "</td></tr></table>";
        return rs;
    }

    protected string BindSupport(string nickname, string server, string fullname, string position)
    {
        string rs = "";
        rs += "<table width='100%'><tr><td>";
        if (server == "0") // yahoo
        {
            rs += "<table align='left'><tr><td style='padding: 0px 4px 0px 3px;'><a href='ymsgr:sendim?" + nickname + "&m=Xin chào bạn'>";
            rs += "<img alt='' src='../images/yahoo.jpg' border='0' align='left' />";
            rs += "</a></td></tr></table>";
            rs += "<p><a href='ymsgr:sendim?" + nickname + "&m=Xin chào bạn'>" + nickname + "</a></p>";
            rs += "<p><br></p>";
        }
        else if (server == "1") // skype
        {
            rs += "<table align='left'><tr><td style='padding: 3px 6px 3px 3px;'><a href='skype:" + nickname + "?chat'>";
            rs += "<img alt='' src='../images/skype.jpg' border='0' align='left' />";
            rs += "</a></td></tr></table>";
            rs += "<p><a class='nickname' href='skype:" + nickname + "?chat'>" + nickname + "</a></p>";
            rs += "<p><br></p>";
        }
        if (fullname != "")
            rs += "<p>Họ tên:<span class='name'>" + fullname + "</span></p>";
        if (position != "")
            rs += "<p>Chức vụ:<span class='name'>" + position + "</span></p>";
        rs += "</td></tr></table>";
        return rs;
    }

    protected string BindProduct(string id)
    {
        string rs = "";
        GssDO.DOProduct DO = getProductById(id);
        if (DO != null)
        {
            GssDO.DOShop shop = getShopById(DO.ShopID);
            rs += "<table width='100%'><tr><td>";
            if (DO.Image != "")
            {
                rs += "<table align='right'><tr><td style='padding: 3px 3px 3px 6px;'><a href='ProductDetail.aspx?id=" + id + "'>";
                rs += "<img alt='' src='ProductThumbnail.aspx?id=" + id + "&width=50' border='0' align='right' onerror=\"this.src='../images/noimage50.gif'\" />";
                rs += "</a></td></tr></table>";
            }
            rs += "<p><a class='title' href='ProductDetail.aspx?id=" + id + "'>" + DO.Title + "</a></p>";
            if (shop != null && shop.IsDeleted == 0)
                rs += "<p><a class='shop' href='ShopDetail.aspx?id=" + shop.PKIDShop + "'>" + shop.Title + "</a></p>";
            double p = DO.Price, r = DO.Price - DO.SaleOff;
            if (p != 0)
            {
                if (r != 0 && r < p)
                {
                    rs += "<p class='price'><s>" + p.ToString("##,###") + " VNĐ</s><br><span>" + r.ToString("##,###") + " VNĐ</span></p>";
                }
                else
                    rs += "<p class='price'><span>" + p.ToString("##,###") + " VNĐ</span></p>";
            }
            else if (p == 0)
                rs += "<p class='price'>Giá liên hệ</p>";
            rs += "</td></tr></table>";
        }
        return rs;
    }

    protected string ShowListProduct(string productType, string caption)
    {
        int type = Convert.ToInt32(productType);
        DataTable dt = GetProductByType(type);
        GssDO.DOProduct DO = new GssDO.DOProduct();
        string rs = "", id = "", image = "", title = "";
        double p = 0, s = 0, r = 0;

        rs = "<ul id=\"contentid" + productType + "\">";
        foreach (DataRow dr in dt.Rows)
        {
            id = dr[DO.PKIDProductColumnName].ToString();
            title = dr[DO.TitleColumnName].ToString();
            image = dr[DO.ImageColumnName].ToString();
            p = Convert.ToDouble(dr[DO.PriceColumnName]);
            s = Convert.ToDouble(dr[DO.SaleOffColumnName]);
            r = p - s;
            if (image != "")
            {
                rs += "<li>";
                rs += "<table align='left' border='0' cellpadding='0' cellspacing='0'><tr><td style='padding: 1px 3px 1px 2px;'><a title='" + title + "' href='ProductDetail.aspx?id=" + id + "'>";
                rs += "<img alt='" + title + "' src='ProductThumbnail.aspx?id=" + id + "&width=40' border='0' align='left' onerror=\"this.src='../images/noimage50.gif'\" />";
                rs += "</a></td></tr></table>";
                rs += "<p class='caption'>" + caption + "</p>";
                if (p != 0)
                {
                    if (r != 0 && r < p)
                    {
                        rs += "<p class='price'><s>" + p.ToString("##,###") + " VNĐ</s><br>" + r.ToString("##,###") + " VNĐ</p>";
                    }
                    else
                        rs += "<p class='price'>" + p.ToString("##,###") + " VNĐ</p>";
                }
                else if (p == 0)
                    rs += "<p class='price'>Giá liên hệ</p>";
                rs += "<p class='title'><a href='ProductDetail.aspx?id=" + id + "'>" + title + "</a></p>";
                GssDO.DOShop shop = getShopById(dr[DO.ShopIDColumnName].ToString());
                if (shop != null && shop.IsDeleted == 0)
                    rs += "<p class='shop'><a href='ShopDetail.aspx?id=" + shop.PKIDShop + "'>" + shop.Title + "</a></p>";
                rs += "</li>";
            }
        }
        rs += "</ul>";
        rs += "<script language=\"JavaScript\">";
        if (dt != null && dt.Rows.Count > 0)
            rs += "var objFlag" + productType + " = 1;";
        else
            rs += "var objFlag" + productType + " = 0;";
        rs += "</script>";
        return rs;
    }

    protected string ReadFashionAdvertise()
    {
        string rs = "";
        try
        {
            System.IO.StreamReader sr = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"App_Data\fashionadv.txt");
            rs = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
        }
        catch (Exception ex)
        {
            string str = ex.ToString();
            rs = "";
            str = "";
        }
        return rs;
    }

    private GssDO.DOProduct getProductById(string id)
    {
        GssDO.DOProduct DO = null;
        try
        {
            GssBL.BLProduct BL = new GssBL.BLProduct();
            DO = BL.getProductByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }

    private GssDO.DOShop getShopById(string id)
    {
        GssDO.DOShop DO = null;
        try
        {
            GssBL.BLShop BL = new GssBL.BLShop();
            DO = BL.getShopByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }

    private DataTable GenerateRandom(int rows)
    {
        DataTable dt = GetListOfProducts();
        Random rnd = new Random();
        int max = dt.Rows.Count;
        foreach (DataRow dr in dt.Rows)
        {
            dr["ProductType"] = rnd.Next(0, max * max);
        }
        DataRow[] drs = dt.Select("IDProduct <> '' ", "ProductType");

        DataTable dtNews = new DataTable();
        dtNews.Columns.Add(new DataColumn("IDProduct"));
        for (int i = 0; i < ((drs.Length > rows) ? rows : drs.Length); i++)
        {
            DataRow dr = dtNews.NewRow();
            dr["IDProduct"] = drs[i]["IDProduct"];
            dtNews.Rows.Add(dr);
        }
        return dtNews;
    }

    private DataTable GetListOfProducts()
    {
        DataTable dt = null;
        try
        {
            GssBL.BLProduct BL = new GssBL.BLProduct();
            dt = BL.getRandomProduct();
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }

    private DataTable GetProductByType(int type)
    {
        DataTable dt = null;
        try
        {
            GssBL.BLProduct BL = new GssBL.BLProduct();
            dt = BL.getProductByType(type);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }

    private DataTable GetListOfSupports()
    {
        DataTable dt = null;
        try
        {
            GssBL.BLOnline BL = new GssBL.BLOnline();
            dt = BL.getAllDataNotDeleted();
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            dt = null;
        }
        return dt;
    }
}
